{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #气温变化趋势"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: temperatureTrend"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #气温变化趋势"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "力扣城计划在两地设立「力扣嘉年华」的分会场，气象小组正在分析两地区的气温变化趋势，对于第 `i ~ (i+1)` 天的气温变化趋势，将根据以下规则判断：\n",
    "- 若第 `i+1` 天的气温 **高于** 第 `i` 天，为 **上升** 趋势\n",
    "- 若第 `i+1` 天的气温 **等于** 第 `i` 天，为 **平稳** 趋势\n",
    "- 若第 `i+1` 天的气温 **低于** 第 `i` 天，为 **下降** 趋势\n",
    "\n",
    "已知 `temperatureA[i]` 和 `temperatureB[i]` 分别表示第 `i` 天两地区的气温。\n",
    "组委会希望找到一段天数尽可能多，且两地气温变化趋势相同的时间举办嘉年华活动。请分析并返回两地气温变化趋势**相同的最大连续天数**。\n",
    "> 即最大的 `n`，使得第 `i~i+n` 天之间，两地气温变化趋势相同\n",
    "\n",
    "**示例 1：**\n",
    ">输入：\n",
    ">`temperatureA = [21,18,18,18,31]`\n",
    ">`temperatureB = [34,32,16,16,17]`\n",
    ">\n",
    ">输出：`2`\n",
    ">\n",
    ">解释：如下表所示， 第 `2～4` 天两地气温变化趋势相同，且持续时间最长，因此返回 `4-2=2`\n",
    "![image.png](https://pic.leetcode-cn.com/1663902654-hlrSvs-image.png){:width=1000px}\n",
    "\n",
    "\n",
    "**示例 2：**\n",
    ">输入：\n",
    ">`temperatureA = [5,10,16,-6,15,11,3]`\n",
    ">`temperatureB = [16,22,23,23,25,3,-16]`\n",
    ">\n",
    ">输出：`3`\n",
    "\n",
    "**提示：**\n",
    "- `2 <= temperatureA.length == temperatureB.length <= 1000`\n",
    "- `-20 <= temperatureA[i], temperatureB[i] <= 40`\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [6CE719](https://leetcode.cn/problems/6CE719/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [6CE719](https://leetcode.cn/problems/6CE719/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[21,18,18,18,31]\\n[34,32,16,16,17]', '[5,10,16,-6,15,11,3]\\n[16,22,23,23,25,3,-16]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        # a,b=[],[]\n",
    "        # for i in range(len(temperatureA)-1):\n",
    "        #     if temperatureA[i] > temperatureA[i+1]:\n",
    "        #         a.append(-1)\n",
    "        #     elif temperatureA[i] == temperatureA[i+1]:\n",
    "        #         a.append(0)\n",
    "        #     elif temperatureA[i] < temperatureA[i+1]:\n",
    "        #         a.append(1)\n",
    "        # for i in range(len(temperatureB)-1):\n",
    "        #     if temperatureB[i] > temperatureB[i+1]:\n",
    "        #         b.append(-1)\n",
    "        #     elif temperatureB[i] == temperatureB[i+1]:\n",
    "        #         b.append(0)\n",
    "        #     elif temperatureB[i] < temperatureB[i+1]:\n",
    "        #         b.append(1)\n",
    "        # con=0\n",
    "        # m=0\n",
    "        # for i in range(len(a)):\n",
    "        #     if a[i]==b[i]:\n",
    "        #         con+=1\n",
    "        #         if con>m:\n",
    "        #             m=con\n",
    "        #     elif a[i]!=b[i]:\n",
    "        #         con=0\n",
    "        # print(a,b)\n",
    "        # return \n",
    "        a=[temperatureA[i]-temperatureA[i-1]  for i in range(1,len(temperatureA))]\n",
    "        b=[temperatureB[i]-temperatureB[i-1]  for i in range(1,len(temperatureB))]\n",
    "        res=cur=0\n",
    "        for i,j in zip(a,b):\n",
    "            if i*j>0 or i==j==0:\n",
    "                cur+=1\n",
    "            else:\n",
    "                cur=0\n",
    "            res=max(res,cur)\n",
    "        return res\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        cnt,ans = 0,0\n",
    "        for i in range(len(temperatureA)-1):\n",
    "            if (temperatureA[i+1] > temperatureA[i]) - (temperatureA[i+1] <\n",
    "             temperatureA[i]) == (temperatureB[i+1] > temperatureB[i]) -                 (temperatureB[i+1]< temperatureB[i]):\n",
    "                cnt += 1\n",
    "                ans = max(cnt,ans)\n",
    "            else:\n",
    "                cnt = 0\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        ans=cnt=0\n",
    "        for(a1,b1),(a2,b2)in pairwise(zip(temperatureA,temperatureB)):\n",
    "            if(a1>a2)-(a1<a2)==(b1>b2)-(b1<b2):\n",
    "                cnt+=1\n",
    "                ans=max(ans,cnt)\n",
    "            else:\n",
    "                cnt=0\n",
    "        return ans\n",
    "   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        ans=cnt=0\n",
    "        for(a1,b1),(a2,b2)in pairwise(zip(temperatureA,temperatureB)):\n",
    "            if(a1>a2)-(a1<a2)==(b1>b2)-(b1<b2):\n",
    "                cnt+=1\n",
    "                ans=max(ans,cnt)\n",
    "            else:\n",
    "                cnt=0\n",
    "        return ans\n",
    "   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        ans = cnt = 0\n",
    "        for (a1, b1), (a2, b2) in pairwise(zip(temperatureA, temperatureB)):\n",
    "            if (a1 > a2) - (a1 < a2) == (b1 > b2) - (b1 < b2):\n",
    "                cnt += 1\n",
    "                \n",
    "            else:\n",
    "                ans = max(ans, cnt)\n",
    "                cnt = 0\n",
    "        else:\n",
    "            ans = max(ans, cnt)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        ans = cnt = 0\n",
    "        for (a1, b1), (a2, b2) in pairwise(zip(temperatureA, temperatureB)):\n",
    "            if (a1 > a2) - (a1 < a2) == (b1 > b2) - (b1 < b2):\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "            else:\n",
    "                cnt = 0\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        label1=[0 for i in range(len(temperatureA)-1)]\n",
    "        label2=[0 for i in range(len(temperatureA)-1)]\n",
    "        dp=[0 for i in range(len(temperatureA)-1)]\n",
    "        for i in range(len(temperatureA)-1):\n",
    "            if temperatureA[i+1]-temperatureA[i]>0:\n",
    "                label1[i]=1\n",
    "            elif temperatureA[i+1]-temperatureA[i]==0:\n",
    "                label1[i]=0\n",
    "            else:\n",
    "                label1[i]=-1\n",
    "            if temperatureB[i+1]-temperatureB[i]>0:\n",
    "                label2[i]=1\n",
    "            elif temperatureB[i+1]-temperatureB[i]==0:\n",
    "                label2[i]=0\n",
    "            else:\n",
    "                label2[i]=-1\n",
    "        if label1[0]==label2[0]:\n",
    "            dp[0]=1\n",
    "        for i in range(1, len(temperatureA)-1):\n",
    "            if label1[i]==label2[i]:\n",
    "                dp[i]=dp[i-1]+1\n",
    "            else:\n",
    "                dp[i]=0\n",
    "        return max(dp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        cnt = 0\n",
    "        ans = 0\n",
    "        n = len(temperatureA)\n",
    "        for i in range(1, n):\n",
    "            if temperatureA[i] == temperatureA[i - 1] and temperatureB[i] == temperatureB[i - 1]:\n",
    "                cnt += 1\n",
    "            elif (temperatureA[i] - temperatureA[i - 1]) * (temperatureB[i] - temperatureB[i - 1]) > 0:\n",
    "                cnt += 1\n",
    "            else:\n",
    "                cnt = 0\n",
    "            ans = max(ans, cnt)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        ans=cnt=0\n",
    "        for(a1,b1),(a2,b2)in pairwise(zip(temperatureA,temperatureB)):\n",
    "            if(a1>a2)-(a1<a2)==(b1>b2)-(b1<b2):\n",
    "                cnt+=1\n",
    "                ans=max(ans,cnt)\n",
    "            else:\n",
    "                cnt=0\n",
    "        return ans\n",
    "   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        def judge(a,b):\n",
    "            if a > b:\n",
    "                return 1\n",
    "            elif b > a:\n",
    "                return -1\n",
    "            else:\n",
    "                0\n",
    "        k = 0\n",
    "        max = 0\n",
    "        for i in range(0, len(temperatureA) - 1):\n",
    "            r1 = judge(temperatureA[i+1], temperatureA[i])\n",
    "            r2 = judge(temperatureB[i+1], temperatureB[i])\n",
    "            if r2 == r1:\n",
    "                k += 1\n",
    "                if max < k:\n",
    "                    max = k\n",
    "            else:\n",
    "                k = 0 \n",
    "        return max\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        res = 0\n",
    "        i = 0\n",
    "        j = 0\n",
    "        while i < len(temperatureA)-1 and j < len(temperatureA)-1:\n",
    "            if temperatureA[j] < temperatureA[j+1] and temperatureB[j] < temperatureB[j+1] or \\\n",
    "                temperatureA[j] == temperatureA[j+1] and temperatureB[j] == temperatureB[j+1] or \\\n",
    "                temperatureA[j] > temperatureA[j+1] and temperatureB[j] > temperatureB[j+1]:\n",
    "                j += 1\n",
    "                res = max(res, j - i)\n",
    "            else:\n",
    "                i += 1\n",
    "                j = i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "#         a,b=[],[]\n",
    "#         n=len(temperatureA)\n",
    "#         for i in range(1,n):\n",
    "#             a.append(i if temperatureA[i]>temperatureA[i-1] else ( 0 if temperatureA[i]==temperatureA[i-1] else -1 ))\n",
    "#             b.append(i if temperatureB[i]>temperatureB[i-1] else ( 0 if temperatureB[i]==temperatureB[i-1] else -1 ))\n",
    "        \n",
    "#         ans,cnt=0,0\n",
    "\n",
    "#         for x,y in zip(a,b):\n",
    "#             if x==y:\n",
    "#                 cnt+=1\n",
    "#             else:\n",
    "#                 cnt=0\n",
    "#             ans=max(ans,cnt)\n",
    "        \n",
    "#         return ans\n",
    "\n",
    "\n",
    "### 灵茶山\n",
    "class Solution:\n",
    "\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "\n",
    "        ans = cnt = 0\n",
    "\n",
    "        for (a1, b1), (a2, b2) in pairwise(zip(temperatureA, temperatureB)):\n",
    "\n",
    "            if (a1 > a2) - (a1 < a2) == (b1 > b2) - (b1 < b2):\n",
    "\n",
    "                cnt += 1\n",
    "\n",
    "                ans = max(ans, cnt)\n",
    "\n",
    "            else:\n",
    "\n",
    "                cnt = 0\n",
    "\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        n = len(temperatureA)\n",
    "        dif1 = [0] * n\n",
    "        dif2 = [0] * n\n",
    "        for i in range(1, n):\n",
    "            if temperatureA[i] > temperatureA[i-1]:\n",
    "                dif1[i-1] = 1\n",
    "            elif temperatureA[i] < temperatureA[i-1]:\n",
    "                dif1[i-1] = -1\n",
    "            if temperatureB[i] > temperatureB[i-1]:\n",
    "                dif2[i-1] = 1\n",
    "            elif temperatureB[i] < temperatureB[i-1]:\n",
    "                dif2[i-1] = -1\n",
    "        dif = [0] * n\n",
    "        for i in range(n-1):\n",
    "            dif[i] = dif1[i]-dif2[i]\n",
    "        \n",
    "        ans = 0\n",
    "        t = 0\n",
    "        for _, i in enumerate(dif):\n",
    "            if _ < n-1:\n",
    "                if i == 0:\n",
    "                    t += 1\n",
    "                    ans = max(ans, t)\n",
    "                else:\n",
    "                    t = 0\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        mx = 0\n",
    "        n = 0\n",
    "        for i in range(1, len(temperatureA)):\n",
    "            tmpa = temperatureA[i] - temperatureA[i-1]\n",
    "            tmpb = temperatureB[i] - temperatureB[i-1]\n",
    "            if tmpa == tmpb:\n",
    "                n += 1\n",
    "                mx = max(n, mx)\n",
    "            else:\n",
    "                if tmpa * tmpb > 0:\n",
    "                    n += 1\n",
    "                    mx = max(n, mx)\n",
    "                else:\n",
    "                    n = 0\n",
    "        return mx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        n=0\n",
    "        m=0\n",
    "        for x in range(len(temperatureA)-1):\n",
    "            if (temperatureA[x] < temperatureA[x+1]) and (temperatureB[x] < temperatureB[x+1]):\n",
    "                n=n+1\n",
    "                if x == len(temperatureA)-2:\n",
    "                     if n>=m:\n",
    "                         m=n\n",
    "            elif (temperatureA[x] == temperatureA[x+1]) and (temperatureB[x] == temperatureB[x+1]):\n",
    "                n=n+1\n",
    "                if x == len(temperatureA)-2:\n",
    "                     if n>=m:\n",
    "                         m=n\n",
    "            elif (temperatureA[x] > temperatureA[x+1]) and (temperatureB[x] > temperatureB[x+1]):\n",
    "                n=n+1\n",
    "                if x == len(temperatureA)-2:\n",
    "                     if n>=m:\n",
    "                         m=n\n",
    "\n",
    "            else:\n",
    "                if n>=m:\n",
    "                    m=n\n",
    "                    n=0\n",
    "                else:\n",
    "                    n=0\n",
    "        return m\n",
    "                \n",
    "                    \n",
    "               "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        a,b=[],[]\n",
    "        n=len(temperatureA)\n",
    "        for i in range(1,n):\n",
    "            a.append(i if temperatureA[i]>temperatureA[i-1] else ( 0 if temperatureA[i]==temperatureA[i-1] else -1 ))\n",
    "            b.append(i if temperatureB[i]>temperatureB[i-1] else ( 0 if temperatureB[i]==temperatureB[i-1] else -1 ))\n",
    "        \n",
    "        ans,cnt=0,0\n",
    "\n",
    "        for x,y in zip(a,b):\n",
    "            if x==y:\n",
    "                cnt+=1\n",
    "            else:\n",
    "                cnt=0\n",
    "            ans=max(ans,cnt)\n",
    "        \n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        n = len(temperatureA)\n",
    "        i = 0\n",
    "        res = 0\n",
    "        cur = 0\n",
    "        while i<n-1:\n",
    "            if (temperatureA[i+1]>temperatureA[i] and temperatureB[i+1]>temperatureB[i]) or(temperatureA[i+1]<temperatureA[i] and temperatureB[i+1]<temperatureB[i]) or (temperatureA[i+1]==temperatureA[i] and temperatureB[i+1]==temperatureB[i]):\n",
    "                cur+=1\n",
    "            else:\n",
    "                res = max(res,cur)\n",
    "                cur = 0\n",
    "            i+=1\n",
    "        return max(res,cur)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        diff1 = [temperatureA[i] - temperatureA[i - 1] for i in range(1, len(temperatureA))]\n",
    "        diff2 = [temperatureB[i] - temperatureB[i - 1] for i in range(1, len(temperatureB))]\n",
    "\n",
    "        res = cnt = 0\n",
    "        for i, j in zip(diff1, diff2):\n",
    "            if i * j > 0 or i == j == 0:\n",
    "                cnt += 1\n",
    "            else:\n",
    "                cnt = 0\n",
    "            res = max(res, cnt)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        label1=[0 for i in range(len(temperatureA)-1)]\n",
    "        label2=[0 for i in range(len(temperatureA)-1)]\n",
    "        dp=[0 for i in range(len(temperatureA)-1)]\n",
    "        for i in range(len(temperatureA)-1):\n",
    "            if temperatureA[i+1]-temperatureA[i]>0:\n",
    "                label1[i]=1\n",
    "            elif temperatureA[i+1]-temperatureA[i]==0:\n",
    "                label1[i]=0\n",
    "            else:\n",
    "                label1[i]=-1\n",
    "            if temperatureB[i+1]-temperatureB[i]>0:\n",
    "                label2[i]=1\n",
    "            elif temperatureB[i+1]-temperatureB[i]==0:\n",
    "                label2[i]=0\n",
    "            else:\n",
    "                label2[i]=-1\n",
    "        if label1[0]==label2[0]:\n",
    "            dp[0]=1\n",
    "        for i in range(1, len(temperatureA)-1):\n",
    "            if label1[i]==label2[i]:\n",
    "                dp[i]=dp[i-1]+1\n",
    "            else:\n",
    "                dp[i]=0\n",
    "        return max(dp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        cnt = 0\n",
    "        ans = 0\n",
    "        n = len(temperatureA)\n",
    "        for i in range(1, n):\n",
    "            if temperatureA[i] == temperatureA[i - 1] and temperatureB[i] == temperatureB[i - 1]:\n",
    "                cnt += 1\n",
    "            elif (temperatureA[i] - temperatureA[i - 1]) * (temperatureB[i] - temperatureB[i - 1]) > 0:\n",
    "                cnt += 1\n",
    "            else:\n",
    "                cnt = 0\n",
    "            ans = max(ans, cnt)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        diff1 = [temperatureA[i] - temperatureA[i-1]   for i in range(1,len(temperatureA))]\n",
    "        diff2 = [temperatureB[i] - temperatureB[i-1]   for i in range(1,len(temperatureB))]\n",
    "        \n",
    "        res = cur = 0\n",
    "        for i,j in zip(diff1,diff2):\n",
    "            if i * j > 0 or i == j == 0:\n",
    "                cur += 1\n",
    "            else:\n",
    "                cur = 0\n",
    "            res = max(res, cur)\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def get_trend(self, a,b):\n",
    "        if a< b:\n",
    "            return 'more'\n",
    "        if a == b:\n",
    "            return 'equal'\n",
    "        return 'less'\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        l = len(temperatureA)\n",
    "        s = 0\n",
    "        m_s = 0\n",
    "        for i in range(l-1):\n",
    "            if  self.get_trend(temperatureA[i], temperatureA[i+1]) == self.get_trend(temperatureB[i], temperatureB[i+1]):\n",
    "                s+= 1\n",
    "                m_s = max(m_s, s)\n",
    "            else:\n",
    "                s = 0\n",
    "        return m_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        ans = cnt = 0\n",
    "        for (a1, b1), (a2, b2) in pairwise(zip(temperatureA, temperatureB)):\n",
    "            if (a1 > a2) - (a1 < a2) == (b1 > b2) - (b1 < b2):\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "            else:\n",
    "                cnt = 0\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "\n",
    "        def compare(x, y):\n",
    "            if x > y:\n",
    "                return -1\n",
    "            elif x < y:\n",
    "                return 1\n",
    "            return 0\n",
    "\n",
    "        n = len(temperatureA)\n",
    "        ans = 0\n",
    "        pre = 0\n",
    "        for i in range(n - 1):\n",
    "            if compare(temperatureB[i + 1], temperatureB[i]) == compare(temperatureA[i + 1], temperatureA[i]):\n",
    "                pre += 1\n",
    "            else:\n",
    "                ans = max(ans, pre)\n",
    "                pre = 0\n",
    "        ans = max(ans, pre)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        cnt,ans = 0,0\n",
    "        for i in range(len(temperatureA)-1):\n",
    "            if (temperatureA[i+1] > temperatureA[i]) - (temperatureA[i+1] <\n",
    "             temperatureA[i]) == (temperatureB[i+1] > temperatureB[i]) -                 (temperatureB[i+1]< temperatureB[i]):\n",
    "                cnt += 1\n",
    "                ans = max(cnt,ans)\n",
    "            else:\n",
    "                cnt = 0\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        length = len(temperatureA)\n",
    "        lis = []\n",
    "        lis2 = []\n",
    "        for i in range(1 ,length):\n",
    "            if temperatureA[i] > temperatureA[i-1]:\n",
    "                lis.append(1)\n",
    "            elif temperatureA[i] == temperatureA[i-1]:\n",
    "                lis.append(0)\n",
    "            else:\n",
    "                lis.append(-1)\n",
    "\n",
    "            if temperatureB[i] > temperatureB[i-1]:\n",
    "                lis2.append(1)\n",
    "            elif temperatureB[i] == temperatureB[i-1]:\n",
    "                lis2.append(0)\n",
    "            else:\n",
    "                lis2.append(-1)\n",
    " \n",
    "        con = 0\n",
    "        maxn = 0\n",
    "        for i in range(length-1):\n",
    "            if lis[i] != lis2[i]:\n",
    "                maxn = max(maxn , con)\n",
    "                con = 0\n",
    "            else:\n",
    "                con = con + 1\n",
    "        else:\n",
    "           maxn = max(maxn , con)\n",
    "\n",
    "        return maxn "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        ans=cnt=0\n",
    "        for(a1,b1),(a2,b2)in pairwise(zip(temperatureA,temperatureB)):\n",
    "            if(a1>a2)-(a1<a2)==(b1>b2)-(b1<b2):\n",
    "                cnt+=1\n",
    "                ans=max(ans,cnt)\n",
    "            else:\n",
    "                cnt=0\n",
    "        return ans\n",
    "   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        ans = cnt = 0\n",
    "        for (a1, b1), (a2, b2) in pairwise(zip(temperatureA, temperatureB)):\n",
    "            if (a1 > a2) - (a1 < a2) == (b1 > b2) - (b1 < b2):\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "            else:\n",
    "                cnt = 0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        res = 0\n",
    "        count = 0\n",
    "        for i in range(len(temperatureA) - 1):\n",
    "            if (temperatureA[i + 1] - temperatureA[i]) * (temperatureB[i + 1] - temperatureB[i]) > 0:\n",
    "                count += 1\n",
    "            elif temperatureA[i + 1] - temperatureA[i] == 0 and temperatureB[i + 1] - temperatureB[i] == 0:\n",
    "                count += 1\n",
    "            else:\n",
    "                res = max(res, count)\n",
    "                count = 0\n",
    "        res = max(res, count)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, A: List[int], B: List[int]) -> int:\n",
    "        n = len(A)\n",
    "        \n",
    "        res = 0\n",
    "        curLen = 0\n",
    "        \n",
    "        for i in range(1, n):\n",
    "            if A[i - 1] < A[i] and B[i - 1] < B[i]:\n",
    "                curLen += 1\n",
    "            elif A[i - 1] == A[i] and B[i - 1] == B[i]:\n",
    "                curLen += 1\n",
    "            elif A[i - 1] > A[i] and B[i - 1] > B[i]:\n",
    "                curLen += 1\n",
    "            else:\n",
    "                curLen = 0\n",
    "            res = max(res, curLen)\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, A: List[int], B: List[int]) -> int:\n",
    "        n = len(A)\n",
    "        res = 0\n",
    "        curLen = 0\n",
    "        \n",
    "        for i in range(1, n):\n",
    "            if A[i - 1] < A[i] and B[i - 1] < B[i]:\n",
    "                curLen += 1\n",
    "            elif A[i - 1] == A[i] and B[i - 1] == B[i]:\n",
    "                curLen += 1\n",
    "            elif A[i - 1] > A[i] and B[i - 1] > B[i]:\n",
    "                curLen += 1\n",
    "            else:\n",
    "                curLen = 0\n",
    "            res = max(res, curLen)\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, t1: List[int], t2: List[int]) -> int:\n",
    "        ans=0\n",
    "        cur=0\n",
    "        for i in range(1,len(t1)):\n",
    "            if (t1[i]-t1[i-1])*(t2[i]-t2[i-1])>0 or (t1[i]-t1[i-1])==(t2[i]-t2[i-1])==0:\n",
    "                cur+=1\n",
    "            else:\n",
    "                ans=max(ans,cur)\n",
    "                cur=0\n",
    "        return max(ans,cur)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        trend = lambda a, b: (b-a)//abs(b-a) if a!=b else 0\n",
    "\n",
    "        ans = 0\n",
    "        diff = 0\n",
    "        for i in range(1, len(temperatureA)):\n",
    "            t1, t2 = trend(temperatureA[i-1], temperatureA[i]), trend(temperatureB[i-1], temperatureB[i])\n",
    "            if t1 == t2:\n",
    "                diff += 1\n",
    "                ans = max(ans, diff)\n",
    "            else:\n",
    "                diff = 0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        ans = cnt = 0\n",
    "        for (a1, b1), (a2, b2) in pairwise(zip(temperatureA, temperatureB)):\n",
    "            if (a1 > a2) - (a1 < a2) == (b1 > b2) - (b1 < b2):\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "            else:\n",
    "                cnt = 0\n",
    "        return ans\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        ans,l=0,0\n",
    "        for i in range(1,len(temperatureA)):\n",
    "            if (temperatureA[i]-temperatureA[i-1])*(temperatureB[i]-temperatureB[i-1])>0 or (temperatureA[i]==temperatureA[i-1] and temperatureB[i]==temperatureB[i-1]):\n",
    "                l+=1\n",
    "            else:\n",
    "                ans=max(ans,l)\n",
    "                l=0\n",
    "        ans=max(ans,l)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        label1=[0 for i in range(len(temperatureA)-1)]\n",
    "        label2=[0 for i in range(len(temperatureA)-1)]\n",
    "        dp=[0 for i in range(len(temperatureA)-1)]\n",
    "        for i in range(len(temperatureA)-1):\n",
    "            if temperatureA[i+1]-temperatureA[i]>0:\n",
    "                label1[i]=1\n",
    "            elif temperatureA[i+1]-temperatureA[i]==0:\n",
    "                label1[i]=0\n",
    "            else:\n",
    "                label1[i]=-1\n",
    "            if temperatureB[i+1]-temperatureB[i]>0:\n",
    "                label2[i]=1\n",
    "            elif temperatureB[i+1]-temperatureB[i]==0:\n",
    "                label2[i]=0\n",
    "            else:\n",
    "                label2[i]=-1\n",
    "        if label1[0]==label2[0]:\n",
    "            dp[0]=1\n",
    "        for i in range(1, len(temperatureA)-1):\n",
    "            if label1[i]==label2[i]:\n",
    "                dp[i]=dp[i-1]+1\n",
    "            else:\n",
    "                dp[i]=0\n",
    "        return max(dp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        n = len(temperatureA)\n",
    "        dp = [1]*n\n",
    "        for i in range(1,n):\n",
    "            a,b = 0,0\n",
    "            if temperatureA[i]>temperatureA[i-1]:\n",
    "                a=1\n",
    "            elif temperatureA[i]<temperatureA[i-1]:\n",
    "                a=2\n",
    "            if temperatureB[i]>temperatureB[i-1]:\n",
    "                b=1\n",
    "            elif temperatureB[i]<temperatureB[i-1]:\n",
    "                b=2\n",
    "            if a==b:\n",
    "                dp[i] += dp[i-1]\n",
    "        return max(dp)-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        def judge(x:int, y:int) -> int:\n",
    "            if x < y:\n",
    "                return -1\n",
    "            elif x== y:\n",
    "                return 0\n",
    "            return 1\n",
    "\n",
    "        trend_a, trend_b = [], []\n",
    "        for i in range(1, len(temperatureA)):\n",
    "            trend_a.append(judge(temperatureA[i], temperatureA[i-1]))\n",
    "            trend_b.append(judge(temperatureB[i], temperatureB[i-1]))\n",
    "        \n",
    "        ans, count = 0, 0\n",
    "        for i in range(len(trend_a)):\n",
    "            if trend_a[i] == trend_b[i]:\n",
    "                count += 1\n",
    "                ans = max(ans, count)\n",
    "            else:\n",
    "                count = 0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        cnt,ans = 0,0\n",
    "        for i in range(len(temperatureA)-1):\n",
    "            if (temperatureA[i+1] > temperatureA[i]) - (temperatureA[i+1] <\n",
    "             temperatureA[i]) == (temperatureB[i+1] > temperatureB[i]) -                 (temperatureB[i+1]< temperatureB[i]):\n",
    "                cnt += 1\n",
    "                ans = max(cnt,ans)\n",
    "            else:\n",
    "                cnt = 0\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, a: List[int], b: List[int]) -> int:\n",
    "        for i in range(len(a) - 1, 0, -1):\n",
    "            if a[i] - a[i-1] > 0:\n",
    "                a[i] = 1\n",
    "            elif a[i] - a[i-1] < 0:\n",
    "                a[i] = -1\n",
    "            else:\n",
    "                a[i] = 0\n",
    "            if b[i] - b[i-1] > 0:\n",
    "                b[i] = 1\n",
    "            elif b[i] - b[i-1] < 0:\n",
    "                b[i] = -1\n",
    "            else:\n",
    "                b[i] = 0\n",
    "        ans = 0\n",
    "        left = 1\n",
    "        for right in range(1, len(a)):\n",
    "            if a[right] == b[right]:\n",
    "                ans = max(ans, right - left + 1)\n",
    "                continue\n",
    "            left = right + 1\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        ans = cnt = 0\n",
    "        for (a1, b1), (a2, b2) in pairwise(zip(temperatureA, temperatureB)):\n",
    "            if (a1 > a2) - (a1 < a2) == (b1 > b2) - (b1 < b2):\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "            else:\n",
    "                cnt = 0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        t1 = [0 if a == b else 1 if a < b else -1 for a, b in pairwise(temperatureA)]\n",
    "        t2 = [0 if a == b else 1 if a < b else -1 for a, b in pairwise(temperatureB)]\n",
    "        c, mc = 0, 0\n",
    "        for x, y in zip(t1, t2):\n",
    "            c = 0 if x != y else c + 1\n",
    "            mc = max(mc, c)\n",
    "        return mc\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        ans = cnt = 0\n",
    "        for (a1, b1), (a2, b2) in pairwise(zip(temperatureA, temperatureB)):\n",
    "            if (a1 > a2) - (a1 < a2) == (b1 > b2) - (b1 < b2):\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "            else:\n",
    "                cnt = 0\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        t1=[]\n",
    "        t2=[]\n",
    "        for i in range(1,len(temperatureA)):\n",
    "            if temperatureA[i]<temperatureA[i-1]:\n",
    "                t1.append(-1)\n",
    "            elif temperatureA[i]==temperatureA[i-1]:\n",
    "                t1.append(0)\n",
    "            elif temperatureA[i]>temperatureA[i-1]:\n",
    "                t1.append(1)\n",
    "            if temperatureB[i]<temperatureB[i-1]:\n",
    "                t2.append(-1)\n",
    "            elif temperatureB[i]==temperatureB[i-1]:\n",
    "                t2.append(0)\n",
    "            elif temperatureB[i]>temperatureB[i-1]:\n",
    "                t2.append(1)\n",
    "        c=0\n",
    "        d=0\n",
    "        for i,j in enumerate(t1):\n",
    "            if t2[i]==j:\n",
    "                c+=1\n",
    "            else:\n",
    "                d=max(d,c)\n",
    "                c=0\n",
    "        d=max(d,c)\n",
    "        return d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "\n",
    "        ans = cnt = 0\n",
    "\n",
    "        for (a1, b1), (a2, b2) in pairwise(zip(temperatureA, temperatureB)):\n",
    "\n",
    "            if (a1 > a2) - (a1 < a2) == (b1 > b2) - (b1 < b2):\n",
    "\n",
    "                cnt += 1\n",
    "\n",
    "                ans = max(ans, cnt)\n",
    "\n",
    "            else:\n",
    "\n",
    "                cnt = 0\n",
    "\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        ans=0\n",
    "        cc=0\n",
    "        maxx=0\n",
    "        for i in range(len(temperatureA)-1):\n",
    "            aa=temperatureA[i+1]-temperatureA[i]\n",
    "            bb=temperatureB[i+1]-temperatureB[i]\n",
    "            if aa>=1:\n",
    "                aa=1\n",
    "            elif aa<0:\n",
    "                aa=-1\n",
    "            if bb>0:\n",
    "                bb=1\n",
    "            elif bb<0:\n",
    "                bb=-1\n",
    "            if aa==bb:\n",
    "                cc+=1\n",
    "            else:\n",
    "                cc=0\n",
    "            maxx=max(maxx,cc)\n",
    "        return maxx\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        def trans(nums):\n",
    "            ans = []\n",
    "            last = nums[0]\n",
    "            for num in nums[1:]:\n",
    "                if num == last:\n",
    "                    ans.append(0)\n",
    "                elif num > last:\n",
    "                    ans.append(1)\n",
    "                else:\n",
    "                    ans.append(-1)\n",
    "                last = num\n",
    "            return ans\n",
    "        ta, tb = trans(temperatureA), trans(temperatureB)\n",
    "        l = ta.__len__()\n",
    "        maximum, index = 0, 0\n",
    "        last = -1\n",
    "        while index < l:\n",
    "            if ta[index] == tb[index]:\n",
    "                maximum = max(maximum, index-last)\n",
    "            else:\n",
    "                last = index\n",
    "            index += 1\n",
    "        return maximum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        length = len(temperatureA) -1\n",
    "        data_list = []\n",
    "        for i in range(length):\n",
    "            if temperatureA[i+1] > temperatureA[i]:\n",
    "                data_list.append('上升')\n",
    "            elif temperatureA[i+1] < temperatureA[i]:\n",
    "                data_list.append('下降')\n",
    "            else:\n",
    "                data_list.append('平稳')\n",
    "        for i in range(length):\n",
    "            if temperatureB[i+1] > temperatureB[i]:\n",
    "                data_list.append('上升')\n",
    "            elif temperatureB[i+1] < temperatureB[i]:\n",
    "                data_list.append('下降')\n",
    "            else:\n",
    "                data_list.append('平稳')\n",
    "        j = length\n",
    "        data_max = 0\n",
    "        count = 0\n",
    "        for i in range(length):\n",
    "            if data_list[i] == data_list[j]:\n",
    "                count += 1\n",
    "                data_max = max(data_max, count)\n",
    "            else:\n",
    "                count = 0\n",
    "            j += 1\n",
    "        return data_max\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        diff1 = [temperatureA[i] - temperatureA[i-1]   for i in range(1,len(temperatureA))]\n",
    "        diff2 = [temperatureB[i] - temperatureB[i-1]   for i in range(1,len(temperatureB))]\n",
    "        \n",
    "        res = cur = 0\n",
    "        for i,j in zip(diff1,diff2):\n",
    "            if i * j > 0 or i == j == 0:\n",
    "                cur += 1\n",
    "            else:\n",
    "                cur = 0\n",
    "            res = max(res, cur)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        len_list = len(temperatureA)\n",
    "        result = 0\n",
    "        k = 0\n",
    "        i = 1\n",
    "        while(i<len_list):\n",
    "            # 判断第i天两地气温变化趋势\n",
    "            if temperatureA[i] > temperatureA[i-1]:\n",
    "                m1 = 1\n",
    "            elif temperatureA[i] == temperatureA[i-1]:\n",
    "                m1 = 0\n",
    "            else:\n",
    "                m1 = -1\n",
    "\n",
    "            if temperatureB[i] > temperatureB[i-1]:\n",
    "                m2 = 1\n",
    "            elif temperatureB[i] == temperatureB[i-1]:\n",
    "                m2 = 0\n",
    "            else:\n",
    "                m2 = -1\n",
    "\n",
    "            # 趋势相同\n",
    "\n",
    "            if m1 == m2:\n",
    "                k += 1\n",
    "                if i == len_list-1:\n",
    "                    if k > result:\n",
    "                        result = k\n",
    "            else:\n",
    "                if k > result :\n",
    "                    result = k\n",
    "                k = 0\n",
    "            i += 1\n",
    "\n",
    "\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        resultA = self.getDirection(temperatureA)\n",
    "        resultB = self.getDirection(temperatureB)\n",
    "        length = len(resultA)\n",
    "        dp = [0] * length\n",
    "        maxVal = 0\n",
    "        for i in range(length):\n",
    "            if resultA[i] == resultB[i]:\n",
    "                if i == 0:\n",
    "                    dp[i] = 1\n",
    "                else:\n",
    "                    dp[i] = dp[i - 1] + 1\n",
    "            else:\n",
    "                dp[i] = 0\n",
    "            maxVal = max(dp[i], maxVal)\n",
    "        return maxVal\n",
    "    \n",
    "    def getDirection(self, temperature: List[int]):\n",
    "        length = len(temperature)\n",
    "        result = []\n",
    "        for i in range(1, length):\n",
    "            if temperature[i] - temperature[i - 1] > 0:\n",
    "                result.append(1)\n",
    "            elif temperature[i] - temperature[i - 1] == 0:\n",
    "                result.append(0)\n",
    "            else:\n",
    "                result.append(-1)\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        a,b=[],[]\n",
    "        for i in range(len(temperatureA)-1):\n",
    "            if temperatureA[i] > temperatureA[i+1]:\n",
    "                a.append(-1)\n",
    "            elif temperatureA[i] == temperatureA[i+1]:\n",
    "                a.append(0)\n",
    "            elif temperatureA[i] < temperatureA[i+1]:\n",
    "                a.append(1)\n",
    "        for i in range(len(temperatureB)-1):\n",
    "            if temperatureB[i] > temperatureB[i+1]:\n",
    "                b.append(-1)\n",
    "            elif temperatureB[i] == temperatureB[i+1]:\n",
    "                b.append(0)\n",
    "            elif temperatureB[i] < temperatureB[i+1]:\n",
    "                b.append(1)\n",
    "        con=0\n",
    "        m=0\n",
    "        for i in range(len(a)):\n",
    "            if a[i]==b[i]:\n",
    "                con+=1\n",
    "                if con>m:\n",
    "                    m=con\n",
    "            elif a[i]!=b[i]:\n",
    "                con=0\n",
    "        print(a,b)\n",
    "        return m\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:\n",
    "        n = len(temperatureA)\n",
    "        dif1 = [0] * n\n",
    "        dif2 = [0] * n\n",
    "        for i in range(1, n):\n",
    "            if temperatureA[i] > temperatureA[i-1]:\n",
    "                dif1[i-1] = 1\n",
    "            elif temperatureA[i] < temperatureA[i-1]:\n",
    "                dif1[i-1] = -1\n",
    "            if temperatureB[i] > temperatureB[i-1]:\n",
    "                dif2[i-1] = 1\n",
    "            elif temperatureB[i] < temperatureB[i-1]:\n",
    "                dif2[i-1] = -1\n",
    "        dif = [0] * n\n",
    "        for i in range(n-1):\n",
    "            dif[i] = dif1[i]-dif2[i]\n",
    "        \n",
    "        last = s = 0\n",
    "        ans = 0\n",
    "        t = 0\n",
    "        for _, i in enumerate(dif):\n",
    "            if _ < n-1:\n",
    "                s += abs(i)\n",
    "                print(i, s, last)\n",
    "                if s == last:\n",
    "                    t += 1\n",
    "                    ans = max(ans, t)\n",
    "                else:\n",
    "                    ans = max(ans, t)\n",
    "                    t = 0\n",
    "                    last = s\n",
    "                # last = s\n",
    "        return ans\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
